package com.zzyl.member.service.impl;

import java.util.*;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONUtil;
import cn.hutool.jwt.JWT;
import cn.hutool.jwt.JWTUtil;
import com.zzyl.framework.web.service.TokenService;
import com.zzyl.member.domain.dto.UserLoginDto;
import com.zzyl.member.domain.vo.UserLoginVo;
import com.zzyl.wechat.service.WeChatService;
import com.zzyl.wechat.service.impl.WeChatServiceImpl;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.member.mapper.FamilyMemberMapper;
import com.zzyl.member.domain.FamilyMember;
import com.zzyl.member.service.IFamilyMemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 老人家属Service业务层处理
 *
 * @author ｙｚｙｌ
 * @date 2025-10-22
 */
@Service
public class FamilyMemberServiceImpl extends ServiceImpl<FamilyMemberMapper, FamilyMember> implements IFamilyMemberService {
    @Autowired
    private FamilyMemberMapper familyMemberMapper;

    @Autowired
    private WeChatService weChatService;

    @Autowired
    private TokenService tokenService;

    private List<String> nictNameList = Arrays.asList("大东北", "大河北", "大陕北", "大台北");

    /**
     * 查询老人家属
     *
     * @param id 老人家属主键
     * @return 老人家属
     */
    @Override
    public FamilyMember selectFamilyMemberById(Long id) {
        return getById(id);
    }

    /**
     * 查询老人家属列表
     *
     * @param familyMember 老人家属
     * @return 老人家属
     */
    @Override
    public List<FamilyMember> selectFamilyMemberList(FamilyMember familyMember) {
        return familyMemberMapper.selectFamilyMemberList(familyMember);
    }

    /**
     * 新增老人家属
     *
     * @param familyMember 老人家属
     * @return 结果
     */
    @Override
    public int insertFamilyMember(FamilyMember familyMember) {
        return save(familyMember) ? 1 : 0;
    }

    /**
     * 修改老人家属
     *
     * @param familyMember 老人家属
     * @return 结果
     */
    @Override
    public int updateFamilyMember(FamilyMember familyMember) {
        return updateById(familyMember) ? 1 : 0;
    }

    /**
     * 批量删除老人家属
     *
     * @param ids 需要删除的老人家属主键
     * @return 结果
     */
    @Override
    public int deleteFamilyMemberByIds(Long[] ids) {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除老人家属信息
     *
     * @param id 老人家属主键
     * @return 结果
     */
    @Override
    public int deleteFamilyMemberById(Long id) {
        return removeById(id) ? 1 : 0;
    }

    @Override
    public UserLoginVo login(UserLoginDto dto) {
        // 获取openId
        String openId = weChatService.getopenID(dto.getCode());
        // 获取手机号
        String phone = weChatService.getPhone(dto.getPhoneCode());
        // 随机昵称
        String NickName = nictNameList.get(0) + RandomStringUtils.randomNumeric(4);
        //查询家属对象，没有则注册
        FamilyMember member = this.lambdaQuery().eq(FamilyMember::getOpenId, openId).one();
        if (ObjectUtil.isEmpty(member)) {
            member = new FamilyMember();
            Collections.shuffle(nictNameList);
            member.setName(NickName);
            member.setOpenId(openId);
        }

        //如果手机号存在，则更新手机号
        if (StrUtil.isNotBlank(member.getPhone())) {
            this.lambdaUpdate().eq(FamilyMember::getId, member.getId())
                    .set(FamilyMember::getPhone, phone)
                    .update();
        } else {
            member.setPhone(phone);
            this.save(member);
        }


        //生成oken
        Map<String, Object> params = new HashMap<>();
        params.put("id", member.getId());
        params.put("phoneNumber", member.getPhone());
        String token = tokenService.createToken(params);

        return UserLoginVo.builder().token(token).nickName(member.getName()).build();
    }
}
